package com.chijun.mform.field.impl;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.create.mode.FormFieldOption;
import com.chijun.mform.field.BaseField;
import com.chijun.mform.process.dapater.ShowMutileOptionAdapter;
import com.example.ccj.b.c.dialog.MultiChoiceDialog;
import com.example.ccj.b.c.dialog.MultiChoiceListener;
import com.example.ccj.b.c.recycler.BaseRecycler;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ToastUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.reactivex.ObservableEmitter;

/**
 * 单项选择
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:43
 */
public class FieldFor04DuoXiangXuanZe extends BaseField {
    public FieldFor04DuoXiangXuanZe(){
        super();
    }
    

    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {
        FormField formField = writeFormClass.formField;
        List<FormFieldOption> formFieldOptionList = writeFormClass.writeFormData.getFormFieldOptionMap().get(formField.getId());

        //提示文本
        if (formField.getHint() != null) writeFormClass.fieldFullContentView.setHint(formField.getHint());
        //输入文本颜色
        if (formField.getTextColor() != 0) writeFormClass.fieldFullContentView.setTextColor(formField.getTextColor());
        //默认文本
        String textFieldZanCunData = getTextFieldZanCunData(context,"fieldId=?",String.valueOf(formField.getId()));
        if (textFieldZanCunData == null) textFieldZanCunData = formField.getDefaultContent();
        if (textFieldZanCunData != null) {
            if (formFieldOptionList != null) {
                saveValue = textFieldZanCunData;
                String[] split = textFieldZanCunData.split(",");
                List<String> ssss = Arrays.asList(split);
                StringBuilder builder = new StringBuilder();
                for (FormFieldOption formFieldOption : formFieldOptionList) {
                    if (ssss.contains(String.valueOf(formFieldOption.id))) {
                        builder.append(formFieldOption.content).append(",");
                    }
                }
                writeFormClass.fieldFullContentView.setText(builder);
                writeFormClass.inputIsOK = true;
            } else {
                LogUtils.e("存在选项但未获取到选项配置！");
            }
        }

        writeFormClass.fieldFullContentView.setKeyListener(null);
        writeFormClass.fieldFullContentView.setFocusable(false);
        writeFormClass.fieldFullContentView.setCheckInterval(2000);
        writeFormClass.fieldFullContentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (formFieldOptionList == null || formFieldOptionList.isEmpty()) {
                    ToastUtils.showShort("该字段未配置选项！");
                    return;
                }

                MultiChoiceDialog<FormFieldOption> dialog = new MultiChoiceDialog<FormFieldOption>(v.getContext(),
                        formFieldOptionList,new ArrayList<>(),
                        new MultiChoiceListener<FormFieldOption>() {
                            @Override
                            public String formatItem(@NonNull FormFieldOption o) {
                                return o.content;
                            }

                            @Override
                            public void onSelect(List<FormFieldOption> list) {
                                StringBuilder builderSave = new StringBuilder();
                                StringBuilder builderShow = new StringBuilder();

                                for (FormFieldOption formFieldOption : list) {
                                    builderSave.append(formFieldOption.id).append(",");
                                    builderShow.append(formFieldOption.content).append(",");
                                }
                                writeFormClass.fieldFullContentView.setText(builderShow.toString());
                                saveValue = builderSave;
                                //回调选项改变事件
                                writeFormClass.optionChangeListener.onChanged(formField.getId(),list.toArray(new FormFieldOption[0]));
                            }
                        });
                dialog.show();
            }
        });

        return writeFormClass.view;
    }


    //获取用于展示表单字段数据的view
    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.setFieldProperty();
        //隐藏字段值
        fullModel.goneContentView();

        //获取选项
        List<FormFieldOption> options = new ArrayList<>();
        List<FormFieldOption> formFieldOptions = fullModel.serverFullData.formFieldOptionMap.get(fullModel.formField.getId());
        StringBuilder fieldValue = new StringBuilder(fullModel.getFieldValue());
        if (formFieldOptions != null && fieldValue != null) {
            //保存的选项为以逗号分隔的选项编号，所以这里给其进行分割
            String[] split = fieldValue.toString().split(",");
            for (String s : split) {
                for (FormFieldOption formFieldOption : formFieldOptions) {
                    if (String.valueOf(formFieldOption.id).equals(s)) {
                        options.add(formFieldOption);
                    }
                }
            }
        } else {
            LogUtils.w("未返回到字段的选项");
        }

        //放入选项展示
        BaseRecycler<FormFieldOption> recyclerView = new BaseRecycler<>(context);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(options,new ShowMutileOptionAdapter(options,fullModel.isHaveModifyPower()));
        fullModel.addCustomControl(recyclerView);


        if (fullModel.isHaveModifyPower()) {
            Map<Long, List<FormFieldOption>> formFieldOptionMap = fullModel.getServerFullData().formFieldOptionMap;
            if (formFieldOptionMap != null) {
                List<FormFieldOption> formFieldOptions1 = formFieldOptionMap.get(fullModel.formField.getId());
                if (formFieldOptions1 != null) {
                    MultiChoiceDialog<FormFieldOption> dialog = new MultiChoiceDialog<FormFieldOption>(fullModel.getContext(),
                            formFieldOptions1,new ArrayList<>(),
                            new MultiChoiceListener<FormFieldOption>() {
                                @Override
                                public String formatItem(@NonNull FormFieldOption o) {
                                    return o.content;
                                }
                                @Override
                                public void onSelect(List<FormFieldOption> list) {
                                    for (FormFieldOption formFieldOption : list) {
                                        fieldValue.append(formFieldOption.id).append(",");
                                        recyclerView.replaceData(list);
                                    }
                                    processModifyFormFieldDataMap.put(fullModel.formField.getId(),fieldValue.toString());
                                    //回调选项改变事件
                                    fullModel.optionChangeListener.onChanged(fullModel.formField.getId(),list.toArray(new FormFieldOption[0]));
                                }
                            });
                    dialog.show();
                } else {
                    LogUtils.w("未获取到多选项的选项！");
                }
            }
        }

        return fullModel.layout;
    }

    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getOptionView());        //选项设置
        emitter.onNext(installClass.getIsMastView());        //是否必填
        emitter.onNext(installClass.getIsCanScanView());     //是否可扫码
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印

        return installClass.v;
    }


}
